package com.example.mvc
import android.view.View
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView
import android.widget.AdapterView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化 Spinner、Button 和 TextView
        val spinner: Spinner = findViewById(R.id.spinner)
        val button: Button = findViewById(R.id.button)
        val textView: TextView = findViewById(R.id.textView)

        // 设置 Spinner 的适配器
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, resources.getStringArray(R.array.list))
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = adapter

        // 设置 Spinner 的选择监听器
        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                // 获取选中的项
                val selectedFeature = parent?.getItemAtPosition(position).toString()
                // 更新 TextView
                textView.text = selectedFeature
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选中任何项时的处理
                textView.text = ""
            }
        }

        // 设置 Button 的点击监听器
        button.setOnClickListener {
            val selectedFeature = spinner.selectedItem.toString()
            val language = ProgramExpert().getLanguage(selectedFeature)
            textView.text = language
        }
    }
}

class ProgramExpert {
    fun getLanguage(feature: String): String = when(feature) {
        "快速" -> "C"
        "容易" -> "Python"
        "新语言" -> "Kotlin"
        "面向对象" -> "Java"
        else -> "Unknown"
    }
}